home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0023_Graphics in Your Listboxes.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  5.7 KB  |  217 lines

  1.  
  2. The ability to place graphics inside ListBoxes and ComboBoxes 
  3. can improve the look of your application and set your user 
  4. interface apart from the others. 
  5.  
  6. Q: How do I stick graphics in a Listbox or ComboBox???
  7.  
  8. Here is an step-by-step example.....
  9.  
  10. 1.  Create a form.
  11.  
  12. 2.  Place a ComboBox and Listbox component on your form.
  13.  
  14. 3.  Change the Style property of the ComboBox component to 
  15. csOwnerDrawVariable and the Style property of the ListBox to 
  16.  
  17. lbOwnerDrawVariable.
  18.  
  19. An Owner-Draw TListBox or TComboBox allows you to display 
  20. both objects (ex. graphics) and strings as the items.  For 
  21. this example, we are adding both a graphic object and a 
  22. string.
  23.  
  24. 4.  Create 5 variables of type TBitmap in the Form's VAR 
  25. section.
  26.  
  27. 5.  Create a Procedure for the Form's OnCreate event.
  28.  
  29. 6.  Create a Procedure for the ComboBox's OnDraw Event.
  30.  
  31. 7.  Create a Procedure for the ComboBox's OnMeasureItem.
  32.  
  33. 8. Free the resources in the Form's OnClose Event.
  34.  
  35.  
  36.  
  37. {START OWNERDRW.PAS}
  38. unit Ownerdrw;
  39.  
  40. interface
  41.  
  42. uses
  43.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  44.   Forms, Dialogs, StdCtrls;
  45.  
  46. type
  47.   TForm1 = class(TForm)
  48.     ComboBox1: TComboBox;
  49.     ListBox1: TListBox;
  50.     procedure FormCreate(Sender: TObject);
  51.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  52.     procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  53.       Rect: TRect; State: TOwnerDrawState);
  54.  
  55.     procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  56.       var Height: Integer);
  57.     procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
  58.       Rect: TRect; State: TOwnerDrawState);
  59.     procedure ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  60.       var Height: Integer);
  61.   private
  62.     { Private declarations }
  63.   public
  64.     { Public declarations }
  65.   end;
  66.  
  67. var
  68.   Form1: TForm1;
  69.   TheBitmap1, TheBitmap2, TheBitmap3, TheBitmap4,
  70.  
  71.   TheBitmap5 : TBitmap;
  72. implementation
  73.  
  74. {$R *.DFM}
  75.  
  76. procedure TForm1.FormCreate(Sender: TObject);
  77. begin
  78.   TheBitmap1 := TBitmap.Create;
  79.   TheBitmap1.LoadFromFile('C:\delphi\images\buttons\globe.bmp');
  80.   TheBitmap2 := TBitmap.Create;
  81.   TheBitmap2.LoadFromFile('C:\delphi\images\buttons\video.bmp');
  82.   TheBitmap3 := TBitmap.Create;
  83.   TheBitmap3.LoadFromFile('C:\delphi\images\buttons\gears.bmp');
  84.   TheBitmap4 := TBitmap.Create;
  85.   TheBitmap4.LoadFromFile('C:\delphi\images\buttons\key.bmp');
  86.  
  87.   TheBitmap5 := TBitmap.Create;
  88.   TheBitmap5.LoadFromFile('C:\delphi\images\buttons\tools.bmp');
  89.   ComboBox1.Items.AddObject('Bitmap1: Globe', TheBitmap1);
  90.   ComboBox1.Items.AddObject('Bitmap2: Video', TheBitmap2);
  91.   ComboBox1.Items.AddObject('Bitmap3: Gears', TheBitmap3);
  92.   ComboBox1.Items.AddObject('Bitmap4: Key', TheBitmap4);
  93.   ComboBox1.Items.AddObject('Bitmap5: Tools', TheBitmap5);
  94.   ListBox1.Items.AddObject('Bitmap1: Globe', TheBitmap1);
  95.   ListBox1.Items.AddObject('Bitmap2: Video', TheBitmap2);
  96.  
  97.   ListBox1.Items.AddObject('Bitmap3: Gears', TheBitmap3);
  98.   ListBox1.Items.AddObject('Bitmap4: Key', TheBitmap4);
  99.   ListBox1.Items.AddObject('Bitmap5: Tools', TheBitmap5);
  100.  
  101. end;
  102.  
  103. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  104. begin
  105.   TheBitmap1.Free;
  106.   TheBitmap2.Free;
  107.   TheBitmap3.Free;
  108.   TheBitmap4.Free;
  109.   TheBitmap5.Free;
  110. end;
  111.  
  112. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  113.   Rect: TRect; State: TOwnerDrawState);
  114.  
  115. var
  116.   Bitmap: TBitmap;
  117.   Offset: Integer;
  118. begin
  119.   with (Control as TComboBox).Canvas do
  120.   begin
  121.     FillRect(Rect);
  122.     Bitmap := TBitmap(ComboBox1.Items.Objects[Index]);
  123.     if Bitmap <> nil then
  124.     begin
  125.       BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  126.                 Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  127.                 Bitmap.Height), clRed);
  128.       Offset := Bitmap.width + 8;
  129.     end;
  130.     { display the text }
  131.     TextOut(Rect.Left + Offset, Rect.Top, Combobox1.Items[Index])
  132.  
  133.   end;
  134. end;
  135.  
  136. procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index:
  137.                                       Integer; var Height: Integer);
  138. begin
  139.   height:= 20;
  140. end;
  141.  
  142. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  143.   Rect: TRect; State: TOwnerDrawState);
  144. var
  145.   Bitmap: TBitmap;
  146.   Offset: Integer;
  147. begin
  148.   with (Control as TListBox).Canvas do
  149.   begin
  150.     FillRect(Rect);
  151.     Bitmap := TBitmap(ListBox1.Items.Objects[Index]);
  152.     if Bitmap <> nil then
  153.  
  154.     begin
  155.       BrushCopy(Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap.Width,
  156.                 Bitmap.Height), Bitmap, Bounds(0, 0, Bitmap.Width,
  157.                 Bitmap.Height), clRed);
  158.       Offset := Bitmap.width + 8;
  159.     end;
  160.     { display the text }
  161.     TextOut(Rect.Left + Offset, Rect.Top, Listbox1.Items[Index])
  162.   end;
  163. end;
  164.  
  165. procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
  166.   var Height: Integer);
  167. begin
  168.   height:= 20;
  169. end;
  170.  
  171. end.
  172.  
  173. {END OWNERDRW.PAS}
  174.  
  175. {START OWNERDRW.DFM}
  176. object Form1: TForm1
  177.   Left = 211
  178.   Top = 155
  179.   Width = 435
  180.   Height = 300
  181.   Caption = 'Form1'
  182.   Font.Color = clWindowText
  183.   Font.Height = -13
  184.   Font.Name = 'System'
  185.   Font.Style = []
  186.   PixelsPerInch = 96
  187.   OnClose = FormClose
  188.   OnCreate = FormCreate
  189.   TextHeight = 16
  190.   object ComboBox1: TComboBox
  191.     Left = 26
  192.     Top = 30
  193.     Width = 165
  194.     Height = 22
  195.     Style = csOwnerDrawVariable
  196.     ItemHeight = 16
  197.     TabOrder = 0
  198.  
  199.     OnDrawItem = ComboBox1DrawItem
  200.     OnMeasureItem = ComboBox1MeasureItem
  201.   end
  202.   object ListBox1: TListBox
  203.     Left = 216
  204.     Top = 28
  205.     Width = 151
  206.     Height = 167
  207.     ItemHeight = 16
  208.     Style = lbOwnerDrawVariable
  209.     TabOrder = 1
  210.     OnDrawItem = ListBox1DrawItem
  211.     OnMeasureItem = ListBox1MeasureItem
  212.   end
  213. end
  214. {END OWNERDRW.DFM}
  215.  
  216.  
  217.